package com.huanxiao.base.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huanxiao.base.R;
import com.huanxiao.base.constant.API;
import com.huanxiao.util.GlideHelper;
import com.huanxiao.util.StringHelper;

/**
 * Created by luoliuqing on 16/10/26.
 * 普通弹框
 */
public class AlertDialogUtil {
    public static android.support.v7.app.AlertDialog.Builder showDialog(Context activity, int titleId, int msgId, int leftBtnTextId, int rightBtnTextId, DialogInterface.OnClickListener leftClickListener, DialogInterface.OnClickListener rightClickListener) {
        return showDialog(activity, StringHelper.ls(titleId),
                StringHelper.ls(msgId),
                leftBtnTextId <= 0 ? "" : StringHelper.ls(leftBtnTextId),
                rightBtnTextId <= 0 ? "" : StringHelper.ls(rightBtnTextId),
                leftClickListener,
                rightClickListener);
    }

    public static android.support.v7.app.AlertDialog.Builder showDialog(Context activity, String title, String msg, String leftBtnText, String rightBtnText, DialogInterface.OnClickListener leftClickListener, DialogInterface.OnClickListener rightClickListener) {
        final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(activity, R.style.AppTheme_Dialog_Alert);
        builder.setTitle(title);
        builder.setMessage(msg);
        if (!TextUtils.isEmpty(leftBtnText)) {
            builder.setNegativeButton(leftBtnText, leftClickListener);
        }
        if (!TextUtils.isEmpty(rightBtnText)) {
            builder.setPositiveButton(rightBtnText, rightClickListener);
        }
        builder.show();
        return builder;
    }


    public static AlertDialog.Builder showEditDialog(Context activity, String title, String msg, String leftBtnText, String rightBtnText, final OnFinishClickListener onFinishClickListener) {

        final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(activity, R.style.AppTheme_Dialog_Alert);
        builder.setTitle(title);

        builder.setMessage(msg);
        View view = LayoutInflater.from(activity).inflate(R.layout.view_edit, null);
        final EditText editText = (EditText) view.findViewById(R.id.et_content);
        builder.setView(view);
        if (!TextUtils.isEmpty(rightBtnText)) {
            builder.setPositiveButton(rightBtnText, new DialogInterface.OnClickListener() {
                @Override

                public void onClick(DialogInterface dialog, int which) {
                    if (TextUtils.isEmpty(editText.getText().toString())) {

                    } else {

                        onFinishClickListener.onFinishListener(0, editText.getText().toString(),null);
                    }
                }
            });
        }
        builder.show();
        return builder;

    }


    public static AlertDialog.Builder showVerifyDialog(Context activity, final OnConfirmClickListener onConfirmClickListener) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.AppTheme_Dialog_Alert);
        builder.setTitle("请输入图形验证码");
        final AlertDialog dialog;
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_verify, null);
        final ImageView imageView = (ImageView) view.findViewById(R.id.iv_verify);
        final EditText editText = (EditText) view.findViewById(R.id.et_verify);
        final Button button = (Button) view.findViewById(R.id.btn_confirm);
        final TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                button.setEnabled(!TextUtils.isEmpty(editText.getText().toString()));
            }
        };
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GlideHelper.display(API.getCaptureUrl(), imageView);
            }
        });
        editText.addTextChangedListener(textWatcher);

        GlideHelper.display(API.getCaptureUrl(), imageView);

        builder.setView(view);


        dialog = builder.create();
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                editText.removeTextChangedListener(textWatcher);
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onConfirmClickListener.confirmClick(editText.getText().toString());
                dialog.dismiss();
            }
        });

        dialog.show();

        return builder;
    }


    public interface OnConfirmClickListener {
        void confirmClick(String verify);
    }


    /**
     * @param context
     * @param strs
     * @param listener
     * @return
     */
    public static Dialog createActionSheet(Activity context, final String[] strs, final OnFinishClickListener listener) {
        final Dialog dialog;
        dialog = new Dialog(context, R.style.ActionSheet);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.dialog_normal, null);
        LinearLayout llContent = (LinearLayout) rootView.findViewById(R.id.ll_content);
        TextView tvCancel = (TextView) rootView.findViewById(R.id.tv_cancel);
        llContent.removeAllViews();
        for (int i = 0; i < strs.length; i++) {
            View view = inflater.inflate(R.layout.item_dialog, null);
            final TextView textView = (TextView) view.findViewById(R.id.tv_content);
            View viewLine = view.findViewById(R.id.view);
            View iv = view.findViewById(R.id.iv_suggest);
            if (strs[i].equals("自动开关店")) {
                iv.setVisibility(View.VISIBLE);
            } else {
                iv.setVisibility(View.GONE);
            }
            textView.setText(strs[i]);
            view.setOnClickListener(new ItemClickListener(i, textView.getText().toString(), dialog, listener));
            if (i == strs.length - 1) {
                viewLine.setVisibility(View.GONE);
            }
            llContent.addView(view);
        }
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        final int cFullFillWidth = 10000;
        rootView.setMinimumWidth(cFullFillWidth);

        Window w = dialog.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        final int cMakeBottom = -1000;
        lp.y = cMakeBottom;
        lp.gravity = Gravity.BOTTOM;
        dialog.onWindowAttributesChanged(lp);
        dialog.setCanceledOnTouchOutside(true);

        dialog.setContentView(rootView);
        return dialog;

    }

    public interface OnFinishClickListener {
        public void onFinishListener(int position, String strs, Dialog dialog);
    }

    public static class ItemClickListener implements View.OnClickListener {
        private int position;
        private String text;
        private Dialog dialog;
        private OnFinishClickListener listener;

        public ItemClickListener(int position, String text, Dialog dialog, OnFinishClickListener listener) {
            this.position = position;
            this.text = text;
            this.dialog = dialog;
            this.listener = listener;
        }

        @Override
        public void onClick(View v) {
            dialog.dismiss();
            listener.onFinishListener(position, text.toString(), dialog);
        }
    }

}
